const bodyParser = require('body-parser')
const userList = [{
  "username": 'admin',
  "password": "admin",
  "role": 'admin'
},{
  "username": 'user',
  "password": "user",
  "role": 'user'
}]
module.exports = (app) => {
  app.use(bodyParser.json())
  app.post('/api/login', (req, res) => {
    // 1. 获取用户名和密码
    const { username, password } = req.body;
    // 2. 验证信息
    const user  =  userList.find(v => v.username === username && v.password === password);
    // 3. 
    if(user) {
      res.send({
        code: 200,
        data: {
          username: user.username,
          role: user.role,
          token: Math.random().toString()
        },
        message: '登录成功'
      })
    } else {
      res.send({
        code: 401,
        message: '账户密码错误'
      })
    }
  })
}